<?php 
	header("Content-type:text/html;charset=utf-8;");
	error_reporting(0);

	// 1、引入连接文件
	include 'link.php';

	if($_GET['id']) {
		// 2、获取对应的订单号
		$order_no = $_GET['id'];

		// 3、获取从上个页面填写的数据
		$receiver = $_POST['receiver'];
		$addr = $_POST['addr'];
		$post_num = $_POST['post_num'];
		$tel = $_POST['tel'];
		$ship_company = $_POST['ship_company'];

		// 4、根据对应的订单号获取相应的信息
		$query = "select * from orders where order_no={$order_no} limit 1";
		$result = mysqli_query($link, $query);
		$rows = mysqli_fetch_array($result);
		if($rows) {
			$isbn = $rows['ISBN'];
			$book_name = $rows['book_name'];
			$order_quantity = $rows['order_quantity'];
			$amount = $rows['amount'];
		}
		
		// 其他字段的值
		$date = date("Y-m-d",time());
		$ship_state = "等待发货";

		// 6、执行插入操作
		$query = "insert into ship 
			(ISBN,book_name,order_quantity,amount,ship_date,receiver,ship_addr,post_num,ship_tel,ship_company,ship_state)
				values 
			($isbn,'$book_name','$order_quantity',$amount,'$date','$receiver','$addr','$post_num','$tel','$ship_company','$ship_state')";

		$result = mysqli_query($link, $query);
		if(!$result) {
			die("<p style='text-align:center;margin-top:50px;'>系统正忙，请稍后再试！</p>");
		} else {
			// 如果用户付款成功，购物车自动清除已付款的订单
			$query = "delete from orders where order_no={$order_no}";
			mysqli_query($link,$query);
			echo "<p style='text-align:center;margin-top:50px;'>付款成功啦亲，快递正在路上飞奔...</p><br />";
			echo "<p style='text-align:center;margin-top:5px;'>
					<a href='index.php'>返回首页</a>　｜　
					<a href='user_zone.php'>个人中心</a>
				</p>";
		}
	} 
	if($_GET['isbn'] && $_GET['num']){
		$isbn = $_GET['isbn'];
		$order_quantity = $_GET['num'];

		$sql = "select * from books where ISBN={$isbn} limit 1";
		$result = mysqli_query($link, $sql);
		$rows = mysqli_fetch_array($result);
		
		if($rows) {
			$book_name = $rows['book_name'];
			$price = $rows['price'];
		}
		
		$amount = $order_quantity * $price;
		$date = date("Y-m-d",time());
		$ship_state = "等待发货";
		
		$receiver = $_POST['receiver'];
		$addr = $_POST['addr'];
		$post_num = $_POST['post_num'];
		$tel = $_POST['tel'];
		$ship_company = $_POST['ship_company'];
		
		// 6、执行插入操作
		$query = "insert into `ship`
			(ISBN,book_name,order_quantity,amount,ship_date,receiver,ship_addr,post_num,ship_tel,ship_company,ship_state)
				values
			($isbn,'$book_name','$order_quantity',$amount,'$date','$receiver','$addr','$post_num','$tel','$ship_company','$ship_state')";

		$result = mysqli_query($link, $query);
		if(!$result) {
			die("<p style='text-align:center;margin-top:50px;'>系统正忙，请稍后再试！</p>");
		} else {
			echo "<p style='text-align:center;margin-top:50px;'>付款成功啦亲，快递正在路上飞奔...</p><br />";
			echo "<p style='text-align:center;margin-top:-20px;'>
					<a href='index.php'>返回首页</a> ｜ 
					<a href='user_zone.php'>个人中心</a>
				</p>";
		}
	}

 ?>